package dln.stat;

import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import net.bat.db.BaseDAO;

import org.springframework.context.ApplicationContext;

public class ExcelExportor {
	static private BaseDAO dao;
	private static ApplicationContext ctx = null;
	private static final String DATE_FORMAT = "yyyy-MM-dd-hh-mm-ss";
	private String realPath;
	private String tempPath;
	
	public String getRealPath() {
		return realPath;
	}

	public void setRealPath(String realPath) {
		this.realPath = realPath;
	} 
	
	public String getTempPath() {
		return tempPath;
	}

	public void setTempPath(String tempPath) {
		this.tempPath = tempPath;
	}
	

	public BaseDAO getDao() {
		return dao;
	}

	public void setDao(BaseDAO dao) {
		this.dao = dao;
	}
	
	private String destFile;
	
	public File buildDestPath() {
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
		File fd = null;  
        try {  
            fd = new File(tempPath);  
            if (!fd.exists()) {  
                fd.mkdirs();  
            }  
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally {  
            fd = null;  
        } 
        destFile = tempPath + File.separator + sdf.format(Calendar.getInstance().getTime()).toString() + "_TJ.xls" ;
		return new File(destFile);
	}
	
    
public void exportExcel(OutputStream out) {
	BufferedOutputStream  dataOut=null;
	try {
        File f0 = new File(destFile);
        byte[] b0 = Byte_File_Object.getBytesFromFile(f0);
        dataOut = new BufferedOutputStream(out);  
        dataOut.write(b0,0,b0.length); 
    	if (dataOut!=null) {
            dataOut.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
}


class Byte_File_Object {

	 public static byte[] getBytesFromFile(File f) {
	        if (f == null) {
	            return null;
	        }
	        try {
	            FileInputStream stream = new FileInputStream(f);
	            ByteArrayOutputStream out = new ByteArrayOutputStream(1000);
	            byte[] b = new byte[1000];
	            int n;
	            while ((n = stream.read(b)) != -1)
	                out.write(b, 0, n);
	            stream.close();
	            out.close();
	            return out.toByteArray();
	        } catch (IOException e) {
	        }
	        return null;
	    }

	 
  public static Object getObjectFromBytes(byte[] objBytes) throws Exception {
  if (objBytes == null || objBytes.length == 0) {
      return null;
  }
  ByteArrayInputStream bi = new ByteArrayInputStream(objBytes);
  ObjectInputStream oi = new ObjectInputStream(bi);
  return oi.readObject();
}

public static byte[] getBytesFromObject(Serializable obj) throws Exception {
  if (obj == null) {
      return null;
  }
  ByteArrayOutputStream bo = new ByteArrayOutputStream();
  ObjectOutputStream oo = new ObjectOutputStream(bo);
  oo.writeObject(obj);
  return bo.toByteArray();
}

}
